package com.lsh.service.impl;

import com.lsh.dao.UserDao;
import com.lsh.dao.UserDatumDao;
import com.lsh.domain.User;
import com.lsh.domain.UserDatum;
import com.lsh.exception.SysException;
import com.lsh.service.SetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

@Service
public class SetServiceImpl implements SetService {

    @Autowired
    UserDao userDao;
    @Autowired
    UserDatumDao userDatumDao;

    /**基本用户基本信息
     * @param userId
     * @return
     */
    @Override
    public UserDatum findByUserDatum(int userId) {
        UserDatum userDatum = userDatumDao.getByUserId(userId);
        String[] array = userDatum.getAddress().split(" ");
        if(array.length == 2){
            userDatum.setProvince(array[0]);
            userDatum.setCity(array[1]);
        }
        return userDatum;
    }

    /**
     * 获取用户所需信息
     * @param userId
     * @return
     */
    @Override
    public User findByUser(int userId){
        User user = userDao.getByUserId(userId);
        return user;
    }

    /**
     * 修改用户密码
     * @param userId
     * @param newPwd
     * @param oldPwd
     * @return
     */
    @Override
    public String updatePassword(String userId, String newPwd, String oldPwd) throws SysException {
        if((newPwd=="" || newPwd==null) || (oldPwd=="" || oldPwd==null)){
            return "旧密码(新密码)均不能为空";
        }
        int res = userDao.updatePassword(userId, newPwd, oldPwd);
        return (res==1) ? "修改密码成功:)" : "旧密码输入错误:(";
    }


}
